<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="warehouseId" label="所属仓库">
        <el-select v-model="queryParams.warehouseId" clearable @keyup.enter.native="handleQuery">
          <el-option label="全部" value=""/>
         <el-option v-for="warehouse in warehouseList.data" :key="warehouse.id" :label="warehouse.name" :value="warehouse.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="库区名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入库区名称" clearable  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="库区类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择库区类型" clearable  @keyup.enter.native="handleQuery">
          <el-option label="全部" value=""/>
          <el-option label="普通区" value="1"/>
          <el-option label="待检区" value="2"/>
          <el-option label="不良品区" value="3"/>
          <el-option label="退货区" value="4"/>
        </el-select>
      </el-form-item>
      <el-form-item label="库区位置" prop="location">
        <el-input placeholder="请输入库区位置" v-model="queryParams.location" clearable  @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="库区状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择库区状态" clearable  @keyup.enter.native="handleQuery">
          <el-option label="全部" value="" />
          <el-option label="启用" value="1" />
          <el-option label="停用" value="0" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['storage:area:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['storage:area:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['storage:area:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['storage:area:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="areaList" @selection-change="handleSelectionChange">


      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="所属仓库" prop="warehouse.name" align="center" ></el-table-column>
      <el-table-column label="库区编码" prop="code" align="center"></el-table-column>
      <el-table-column label="库区名称" prop="name" align="center"></el-table-column>
      <el-table-column label="库区类型" prop="type" align="center" :formatter="formatStorageAreaType"></el-table-column>
      <el-table-column label="库区位置" prop="location" align="center"></el-table-column>
      <el-table-column label="库区状态" prop="status" align="center" :formatter="formatStorageAreaStatus"></el-table-column>
      <el-table-column label="详细数据" type="expand" align="center" width="100px">

      <template #default="props">
        <table style="margin: auto 50px; width: 100%; border-collapse: collapse;">
          <tr>
            <td style="width: 50%; vertical-align: top; padding: 8px;">
              <b>备注：</b>{{ props.row.remark }}<br>
              <b>创建时间：</b>{{ props.row.createdAt }}<br>
              <b>更新时间：</b>{{ props.row.updatedAt }}<br>
            </td>
            <td style="width: 50%; vertical-align: top; padding: 8px;">

            </td>
          </tr>
        </table>
      </template>



      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['storage:area:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['storage:area:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改库区信息管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属仓库" prop="warehouseId" label-width="100px">
          <el-select v-model="form.warehouseId" placeholder="请选择所属仓库"  >
            <el-option v-for="warehouse in warehouseList.data" :key="warehouse.id" :label="warehouse.name" :value="warehouse.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="库区编码" prop="code" label-width="100px" >
          <el-input v-model="form.code" placeholder="请输入库区编码"/>
        </el-form-item>
        <el-form-item label="库区名称" prop="name" label-width="100px" >
          <el-input v-model="form.name" placeholder="请输入库区名称"/>
        </el-form-item>
        <el-form-item label="库区类型" prop="type" label-width="100px">
          <el-select v-model="form.type" placeholder="请选择库区类型" >
              <el-option label="普通区" :value="1" />
              <el-option label="待检区" :value="2" />
              <el-option label="不良品区" :value="3" />
              <el-option label="退货区" :value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="库区位置" prop="location" label-width="100px" >
          <el-input v-model="form.location" placeholder="请输入库区位置"/>
        </el-form-item>
        <el-form-item label="库区总储位" prop="capacity" label-width="100px">
          <el-input-number v-model="form.capacity"/>
        </el-form-item>
        <el-form-item label="库区状态" prop="status" label-width="100px">
          <el-radio-group v-model="form.status">
            <el-radio :label="0">停用</el-radio>
            <el-radio :label="1">启用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark" label-width="100px">
          <el-input type="textarea" placeholder="请输入备注" v-model="form.remark"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listArea,
  getArea,
  delArea,
  addArea,
  updateArea,
  getWarehouse,
  getWarehouseOfIdAndName,
} from "@/api/storage/area"


export default {
  name: "Area",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库区信息管理表格数据
      areaList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 仓库信息数组
      warehouseList:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    }
  },
  created() {
    this.getList()
  },
  methods: {

    // 格式化库区状态
    formatStorageAreaStatus(row){
      const statusMap = {
        0: '停用',
        1: '启用',
      };
      return statusMap[row.status] || row.status;
    },
    // 格式化仓库类型
    formatStorageAreaType(row){
      const typeMap = {
        1: '普通区',
        2: '待检区',
        3: '不良品区',
        4: '退货区',
      };
      return typeMap[row.type] || row.type;
    },
    /** 查询库区信息管理列表 */
    getList() {
      this.loading = true

      console.log('this.queryParams：',this.queryParams);

      listArea(this.queryParams).then(response => {
        this.areaList = response.rows
        this.total = response.total

        //查询仓库数据绑定到搜索部分的下拉框
        getWarehouse().then(response => {
          this.warehouseList = response
        })
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        warehouseId: null,
        code: null,
        name: null,
        type: null,
        location: null,
        capacity: null,
        status: null,
        remark: null,
        createdAt: null,
        updatedAt: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加库区信息管理"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getArea(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改库区信息管理"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateArea(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addArea(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除库区信息管理编号为"' + ids + '"的数据项？').then(function () {
        return delArea(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('storage/area/export', {
        ...this.queryParams
      }, `area_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
